/**
 * 
 * Created On:May 16, 2009 12:50:03 PM
 */
package webapp.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import engine.service.UserManager;


/**
 * @author Neev Information Technologies Pvt. Ltd.
 * 
 * <p>
 * <a href="LoginAction.java.html"><i>View Source</i></a>
 * </p>
 * 
 */
public class LoginAction implements SessionAware, ServletRequestAware,
		ServletResponseAware, ServletContextAware {

	private String userName;

	private String password;

	private String newPassword;

	private String confirmNewPassword;

	private ServletContext servletContext;

	private HttpServletRequest servletRequest;

	private HttpServletResponse servletResponse;

	private Map sessionMap;

	private UserManager userManager;


	/**
	 * @return the userManager
	 */
	public UserManager getUserManager() {
		return userManager;
	}

	/**
	 * @param userManager
	 *            the userManager to set
	 */
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	/**
	 * This method will check if the User is logged in and redirect to the
	 * User's home page else it will redirect to the general home place
	 * 
	 * @return the page to be shown to user based on the login status
	 */
	public String home() {
		String referrer = servletRequest.getHeader("referer");
		// User sessionUser = (User) getSessionMap().get(
		//			SuperlocalConstants.SESSION_USER);
//		if (/* referrer != null && */sessionUser != null) {
//			return "logged_in";
//		}
		return "guest";
	}

	/**
	 * The onlineUserCount is accurate only if we also write a
	 * javax.servlet.http.HttpSessionListener implementation and decrement the
	 * onlineUserCount attribute value in its sessionDestroyed method, which is
	 * called by the container when a user session is inactive for a certain
	 * period of time.
	 */

	public String logout() {
		if (sessionMap instanceof SessionMap) {
			((SessionMap) sessionMap).invalidate();
		}
		int onlineUserCount = 0;
		synchronized (servletContext) {
			try {
				onlineUserCount = (Integer) servletContext
						.getAttribute("onlineUserCount");
			} catch (Exception e) {
			}
			servletContext.setAttribute("onlineUserCount", onlineUserCount - 1);
		}
		return "success";
	}

	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}

	public void setSession(Map map) {
		this.sessionMap = map;
	}

	public void setServletResponse(HttpServletResponse servletResponse) {
		this.servletResponse = servletResponse;
	}

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @param userName
	 *            the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the sessionMap
	 */
	public Map getSessionMap() {
		return sessionMap;
	}

	/**
	 * @param sessionMap
	 *            the sessionMap to set
	 */
	public void setSessionMap(Map sessionMap) {
		this.sessionMap = sessionMap;
	}

	/**
	 * @return the servletContext
	 */
	public ServletContext getServletContext() {
		return servletContext;
	}

	/**
	 * @return the servletRequest
	 */
	public HttpServletRequest getServletRequest() {
		return servletRequest;
	}

	/**
	 * @return the servletResponse
	 */
	public HttpServletResponse getServletResponse() {
		return servletResponse;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmNewPassword() {
		return confirmNewPassword;
	}

	public void setConfirmNewPassword(String confirmNewPassword) {
		this.confirmNewPassword = confirmNewPassword;
	}

}
